package com.gxc.authcenter.system.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.gxc.authcore.system.domain.Param;
import com.gxc.authcore.system.service.ParamService;
import com.gxc.authcore.system.util.ParamUtil;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;
import com.gxc.webcore.util.JsonUtil;
import com.gxc.webcore.util.RequestUtil;
import com.gxc.webcore.util.StringUtil;

@Controller("system/paramController")
public class ParamController {

	@Autowired
	ParamService paramService;

	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("system/param/param_list.vm");
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ModelAndView grid(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = RequestUtil.getInMap(request);
		PageInfo pageInfo = PageInfo.getFromRequest(request);
		PageList pageList = paramService.listPage(map, pageInfo);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", pageList);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	public ModelAndView create(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("system/param/param_create.vm");
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView createSave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = new HashMap();
		Param param = (Param) RequestUtil.getInObject(request, Param.class);
		param.setId("param_" + StringUtil.getUUID());
		int i = paramService.create(param);
		map.put("status", "success");
		map.put("data", i);
		String result = JsonUtil.object2json(map);
		response.getWriter().write(result);
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView edit(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Param param = paramService.retrieve(id);
		Map model = new HashMap();
		model.put("param", param);
		return new ModelAndView("system/param/param_edit.vm", model);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView editSave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		Param param = (Param) RequestUtil.getInObject(request, Param.class);
		int i = paramService.update(param);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String id = request.getParameter("id");
		int i = paramService.delete(id);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView deleteBatch(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String id = request.getParameter("id");
		String[] id_array = id.split(",");
		int i = paramService.delete(id_array);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Param param = paramService.retrieve(id);
		Map model = new HashMap();
		model.put("param", param);
		return new ModelAndView("system/param/param_view.vm", model);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ModelAndView reload(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ParamUtil.reload();
		Map result = new HashMap();
		result.put("status", "success");
		String resultString = null;
		String jsonpCallback = request.getParameter("jsonpCallback");
		if (StringUtil.isTrimEmpty(jsonpCallback)) {
			resultString = JsonUtil.object2json(result);
		} else {
			resultString = JsonUtil.object2jsonp(result, jsonpCallback);
		}
		response.getWriter().write(resultString);
		return null;
	}

}
